var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      id:'',
      detail:'',
      pjlist:[],
      full:'',
      half:'',
      empty:'',
      playing:[],
      now_page:1,
      totalpage:'',
      oneButton: [{ text: '确定' }],
      tixingshow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({
      id:id
    })
    this.getdetail(id);
  },

  // 获取报价详情
  getdetail:function(id){
      var that = this;
      var param = {
        url:"api/index/grabOrderDetail",
        data:{
          grab_orders_id:id
        },
        success:function(e){
          console.log(e)
          
          if (e.data.data.score){
            var score = e.data.data.score;
            var full = Math.floor(score);
            var half;
            var empty;
            if (parseInt(score) == score) {
              half = 0
            } else {
              half = 1
            }
            empty = 5 - full - half;
          }else{
            var full = 0;
            var half = 0;
            var empty = 5;
          }
          var vlen = e.data.data.voice.length;
          var playing = that.data.playing;
          for(var i=0;i<vlen;i++){
              playing[i]=0
          }
          that.getpjlist(e.data.data.shop_id);
          that.setData({
            detail:e.data.data,
            full:full,
            empty:empty,
            half:half,
            playing:playing
          })
        }
      };
      app.post(param);
  },
  getpjlist: function (shopid) {
    var that = this;
    var param = {
      url: "api/index/getEvaluations",
      data: {
        shop_id: shopid,
        now_page:that.data.now_page
      },
      success: function (e) {
        console.log(e)
        var list = e.data.data.list;
        for (var i = 0; i < list.length; i++) {
          var score = list[i].score;
          var full = score / 2;
          var empty = 5 - full;
          console.log(full)
          list[i].full = full;
          list[i].empty = empty;
        }
        that.setData({
          pjlist: list
        })
      }
    };
    app.post(param);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  payit:function(){
    var userid = wx.getStorageSync('userId');
    var id = this.data.id;
    var that = this;
    var param = {
      url:"api/index/payOrder",
      data:{
        user_id:userid,
        grab_orders_id:id
      },
      success:function(e){
        console.log(e)
        if (e.data.code == 1) {
          var nonceStr = e.data.data.pay_data.nonceStr;
          var packagenum = e.data.data.pay_data.package;
          var paySign = e.data.data.pay_data.paySign;
          var signType = e.data.data.pay_data.signType;
          var timeStamp = e.data.data.pay_data.timeStamp;
          var order_id = e.data.data.order_id;
          wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: nonceStr,
            package: packagenum,
            signType: signType,
            paySign: paySign,
            success(res) {
              console.log(res)
              that.successpay(order_id)
            },
            fail(res) {

            }
          })
        }
      }
    };
    app.post(param);
  },

  // 支付成功
  successpay: function (id) {
    var param = {
      url: "api/index/paySuccess",
      data: {
        order_id: id
      },
      success: function (e) {
        console.log(e)
        wx.showToast({
          title: e.data.msg,
          icon: "none",
          duration: 2000
        })
        if (e.data.code == 1) {
          this.setData({
            tixingshow:true
          })
          
        }
      }
    };
    app.post(param);
  },

  taptixing:function(){
    this.setData({
      tixingshow: false
    })
    
  },

  // 播放录音
  luyinplay: function (e) {
    var luyinsrc = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var myaudio = wx.createInnerAudioContext();
    myaudio.src=luyinsrc;
    var playing = this.data.playing;
    for(var i=0;i<playing.length;i++){
      playing[i] = 0;
    }
    if (playing[index] == 0) {
      myaudio.play();
      playing[index] = 1;
      this.setData({
        playing:playing
      })
      myaudio.onEnded(() => {
        playing[index]=0
        this.setData({
          playing: playing
        })
      })

    } else {
      myaudio.pause();
      playing[index] = 0;
      this.setData({
        playing: playing
      })
    }

  },
  daohang:function(){
    var detail = this.data.detail;
    wx.openLocation({
      latitude: parseFloat(detail.s_latitude),
      longitude: parseFloat(detail.s_longitude),
      scale: 18,
      name: detail.shop_name,
      address: detail.address
    })  
  },
  onReachBottom: function () {
    var that = this;
    var shopid = wx.getStorageSync('shop_id');
    // 显示加载图标
    // 页数+1
    var now_page = parseInt(that.data.now_page)+1;
    if (now_page > parseInt(that.data.totalpage)) {
      wx.showToast({
        title: '没有更多数据了',
        icon: "none",
        duration: 2000
      })
      return false;
    } else {
      wx.showLoading({
        title: '玩命加载中',
      })
      
    var param = {
      url: "api/index/getEvaluations",
      data: {
        shop_id: shopid,
        now_page: now_page
      },
      success: function (e) {
        wx.hideLoading();
        var scores = [];
        var full;
        var empty;
        var list = e.data.data.list;
        for (var i = 0; i < list.length; i++) {
          var score = list[i].score;
          full = score / 2;
          empty = 5 - full;
          list[i].scores.full = full;
          list[i].scores.empty = empty;
        }
        that.setData({
          pjlist: that.data.pjlist.concat(list),
          now_page:now_page
        })
      }
    };
    app.post(param);
    }
  },
})